import Vue from 'vue';
import Fy from '@/uni_modules/fy-showModal/index.js';
import http from '@/common/http.js';

const utils = {
	toast: function(text, duration, success) {
		uni.showToast({
			title: text || "出错啦~",
			icon: success ? 'success' : 'none',
			duration: duration || 2000
		});
	},
	modal: function(title, content, showCancel, callback, confirmColor, confirmText) {
		Fy.showModal({
			title: title,
			content: content,
			showCancel: showCancel,
			cancelColor: "#ffaa00",
			cancelText: "取消",
			confirmColor: confirmColor || "#0081ff",
			confirmText: confirmText || "确定"
		}).then(() => {
			callback && callback(true);
		}).catch(() => {
			callback && callback(false);
		});
	},
	showLoading: function(title, mask = true) {
		uni.showLoading({
			mask: mask,
			title: title || '请稍候...'
		})
	},
	hideLoading: function() {
		uni.hideLoading();
	},
	isAndroid: function() {
		const res = uni.getSystemInfoSync();
		return res.platform.toLocaleLowerCase() == "android";
	},
	isPhoneX: function() {
		const res = uni.getSystemInfoSync();
		let iphonex = false;
		let models = ['iphonex', 'iphonexr', 'iphonexsmax', 'iphone11', 'iphone11pro', 'iphone11promax']
		const model = res.model.replace(/\s/g, "").toLowerCase();
		if (models.includes(model)) {
			iphonex = true;
		}
		return iphonex;
	},
	// 重置对象
	reset: function(obj, val, data) {
		for (let p in obj) {
			obj[p] = data && data[p] || val;
		}
	},
	// 获取登录信息
	authInfo: function() {
		return uni.getStorageSync(http.authInfoName()) || {};
	},
	// 获取登录信息中的用户信息
	userInfo: function() {
		return utils.authInfo().user || {};
	},
	// 判断是否有角色
	hasRole(role) {
		let authInfo = this.authInfo() || {};
		let roleCodes = authInfo.roles || [];
		if (roleCodes.length === 0) {
			roleCodes.push("ROLE_USER");
		}
		return role === roleCodes[0];
	},
	// 判断是否有角色
	hasRoles(roles) {
		let authInfo = this.authInfo() || {};
		let roleCodes = authInfo.roles || [];
		if (roleCodes.length === 0) {
			roleCodes.push("ROLE_USER");
		}
		return !!roles.includes(roleCodes[0]);
	},
	// 本地local storage 缓存, expire单位秒
	setCache(key, data, expire) {
		let cacheData = {
			data: data,
			expire: expire || null,
			time: new Date()
		};
		uni.setStorageSync("cache_" + key, JSON.stringify(cacheData));
	},
	// 获取本地local storage 缓存
	getCache(key) {
		let str = uni.getStorageSync("cache_" + key) || '{}';
		let cacheData = JSON.parse(str);
		if (cacheData.data != undefined && cacheData != null) {
			if (cacheData.expire) {
				let time = dayjs(cacheData.time).add(cacheData.expire, 'second');
				if (time.isBefore(dayjs(new Date()))) {
					uni.removeStorageSync("cache_" + key);
					return null;
				} else {
					return cacheData.data;
				}
			} else {
				return cacheData.data;
			}
		}
		return null;
	},
	// 删除缓存
	remCache(key) {
		uni.removeStorageSync("cache_" + key);
	},
	// 返回tui-modal对象
	tuiModal(modal, title, content, showCancel, callback, confirmText, confirmColor, cancelText, cancelColor) {
		let button = [];
		if (showCancel) {
			button.push({
				text: cancelText || "取消",
				type: cancelColor || "gray",
				plain: false,
				r: "cancel",
			});
		}
		button.push({
			text: confirmText || "确定",
			type: confirmColor || "primary",
			plain: false,
			r: "confirm"
		});
		let cb = obj => {
			let btn = button[obj.index] || {};
			if (btn.r == "confirm") {
				callback && callback(true);
			} else {
				callback && callback(false);
			}
			Vue.set(modal, "show", false);
		}
		Vue.set(modal, "title", title);
		Vue.set(modal, "content", content);
		Vue.set(modal, "button", button);
		Vue.set(modal, "callback", cb);
		Vue.set(modal, "show", true);
	}
}

Vue.prototype.$utils = utils;
export default utils;